{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# import"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "IPython.notebook.set_autosave_interval(60000)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Autosaving every 60 seconds\n"
     ]
    }
   ],
   "source": [
    "%autosave 60\n",
    "import webbrowser\n",
    "import pyperclip\n",
    "import pyautogui\n",
    "import subprocess\n",
    "import requests\n",
    "import bs4\n",
    "import logging\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.keys import Keys\n",
    "from dir_objectName import dir_objectName\n",
    "logging.basicConfig(level=logging.INFO,\n",
    "                    format=\" %(asctime)s - %(levelname)s - %(message)s \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# open web\n",
    "url = \"https://hellogithub.com/periodical/volume/58/\"\n",
    "webbrowser.open(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 每次函数调用后暂停一秒\n",
    "pyautogui.PAUSE = 1\n",
    "# 启动自动防故障功能\n",
    "pyautogui.FAILSAFE = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Point(x=1456, y=997)"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pyautogui.position()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "pyautogui.moveTo(x=499, y=184, duration=2.5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "pyautogui.press(['esc'])\n",
    "pyautogui.hotkey('ctrlleft', 'end')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<subprocess.Popen at 0x2a491815e50>"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "subprocess.Popen(['start', 'HelloGitHub.md'], shell=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "pyautogui.press(['enter', 'enter'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'111' | pyautogui.hotkey('ctrlleft', 'v')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "comicElem [<a class=\"button-small pure-button pure-button-primary\" href=\"/periodical/volume/57/\">上一期</a>]\n",
      "https://hellogithub.com/periodical/volume/57/\n"
     ]
    }
   ],
   "source": [
    "# open web\n",
    "url = \"https://hellogithub.com/periodical/volume/58/\"\n",
    "webbrowser.open(url)\n",
    "res = requests.get(url)\n",
    "res.raise_for_status()\n",
    "soup = bs4.BeautifulSoup(res.text)\n",
    "# Find the URL of the comic image.\n",
    "comicElem = soup.select('.pre a')\n",
    "print('comicElem', comicElem)\n",
    "if comicElem == []:\n",
    "    logging.info('Could not find comic image.')\n",
    "else:\n",
    "    comicUrl = 'https://hellogithub.com' + comicElem[0].get('href')\n",
    "    webbrowser.open(comicUrl)\n",
    "    # Download the image.\n",
    "    logging.info('Downloading image %s...' % (comicUrl))\n",
    "    print(comicUrl)\n",
    "    try:\n",
    "        res = requests.get(comicUrl)\n",
    "    except:\n",
    "        psass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://hellogithub.com/periodical/volume/58/\" # starting url\n",
    "os.makedirs('hellogithub', exist_ok=True) # store comics in ./xkcd\n",
    "while True:\n",
    "    # Download the page.\n",
    "    print('Downloading page %s...' % url)\n",
    "    res = requests.get(url)\n",
    "    res.raise_for_status()\n",
    "    soup = bs4.BeautifulSoup(res.text)\n",
    "    # Find the URL of the comic image.\n",
    "    comicElem = soup.select('.pre a')\n",
    "    if comicElem == []:\n",
    "        logging.info('Could not find comic image.')\n",
    "    else:\n",
    "        comicUrl = 'http:' + comicElem[0].get('src')\n",
    "        # Download the image.\n",
    "        logging.info('Downloading image %s...' % (comicUrl))\n",
    "        try:\n",
    "            res = requests.get(comicUrl)\n",
    "            res.raise_for_status()\n",
    "        except Exception as err:\n",
    "            logging.error(str(err) + ': ' + comicUrl)\n",
    "        # Save the image to ./xkcd.\n",
    "        imageFile = open(os.path.join('xkcd', os.path.basename(comicUrl)), 'wb')\n",
    "        for chunk in res.iter_content(100000):\n",
    "            imageFile.write(chunk)\n",
    "        imageFile.close()\n",
    "    # Get the Prev button's url.\n",
    "    prevLink = soup.select('a[rel=\"prev\"]')[0]\n",
    "    url = 'http://xkcd.com' + prevLink.get('href')\n",
    "    if url.endswith('#'):\n",
    "        logging.info('End mark：' + url)\n",
    "print('Done.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "volumes = []\n",
    "for i in range(1, 59):\n",
    "    volumes.append(\"https://hellogithub.com/periodical/volume/{:0>2d}/\" .format(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "chromeDriverPath = \"D:\\D00-软件&软件公司\\D10网络工具\\D11浏览器\\chromedriver_win32/chromedriver.exe\"\n",
    "#  浏览器启动\n",
    "browser = webdriver.Chrome(chromeDriverPath)\n",
    "#  将浏览器指向 \n",
    "browser.get('https://www.google.com')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Point(x=573, y=976)"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pyautogui.position()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<subprocess.Popen at 0x2a48ff4ddf0>"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# subprocess.Popen(['start', 'HelloGitHub.md'], shell=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [],
   "source": [
    "pyautogui.hotkey('altleft', 'tab')\n",
    "pyautogui.press('enter')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [],
   "source": [
    "pyautogui.click(x=537, y=444)\n",
    "time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(1, 5):\n",
    "    pyautogui.scroll(-1000000)\n",
    "    pyautogui.scroll(-1000000)\n",
    "time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "pyautogui.keyDown('shiftleft')\n",
    "pyautogui.click(x=573, y=976)\n",
    "pyautogui.keyUp('shiftleft')\n",
    "time.sleep(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "pyautogui.hotkey('ctrlleft', 'x')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
